package com.mole.rbac.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.mole.rbac.dao.StudentDao;
import com.mole.rbac.entity.Student2;

@Repository
public class StudentDaoImpl implements StudentDao{
	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Override
	public int insert(Student2 student) {
		String sql = "insert into t_student(name,age,sex) values(?,?,?)";
		try {
			return jdbcTemplate.update(sql, student.getName(),student.getAge(),student.getSex());
		} catch(DataAccessException e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}

	@Override
	public int update(Student2 student) {
		String sql = "update t_student set name=?,age=?,sex=? where id=?";
		try {
			return jdbcTemplate.update(sql, student.getName(),student.getAge(),student.getSex(),student.getId());
		} catch(DataAccessException e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}

	@Override
	public int deleteById(Long id) {
		String sql = "delete from t_student where id=?";
		try {
			return jdbcTemplate.update(sql, id);
		} catch(DataAccessException e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}

	@Override
	public Student2 selectById(Long id) {
		String sql = "select * from t_student where id=?";
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			map = jdbcTemplate.queryForMap(sql, new Object[]{id});
			String name = map.get("name").toString();
			int age = Integer.parseInt(map.get("age").toString());
			int sex = Integer.parseInt(map.get("sex").toString());
			return new Student2(id, name, age, sex);
		} catch (DataAccessException e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}

	@Override
	public List<Student2> selectAll() {
		String sql = "select * from t_student";
		List<Student2> listStudent = new ArrayList<Student2>();
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		try {
			list = jdbcTemplate.queryForList(sql);
			
			for(Map<String, Object> item : list) {
				long id = Long.parseLong(item.get("id").toString());
				String name = item.get("name").toString();
				int age = Integer.parseInt(item.get("age").toString());
				int sex = Integer.parseInt(item.get("sex").toString());
				Student2 student2 = new Student2(id, name, age, sex);
				listStudent.add(student2);
			}
			return listStudent;
		} catch (DataAccessException e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}

	@Override
	public List<Student2> selectByPage(int pageIndex, int pageSize) {
		if(pageIndex<=0) {
			pageIndex = 0;
		} else {
			pageIndex = pageIndex -1;
		}
		String sql = "select * from t_student limit ?,?";
		List<Student2> listStudent = new ArrayList<Student2>();
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		try {
			list = jdbcTemplate.queryForList(sql, new Object[] {pageIndex, pageSize});
			for(Map<String, Object> item : list) {
				long id = Long.parseLong(item.get("id").toString());
				String name = item.get("name").toString();
				int age = Integer.parseInt(item.get("age").toString());
				int sex = Integer.parseInt(item.get("sex").toString());
				Student2 student2 = new Student2(id, name, age, sex);
				listStudent.add(student2);
			}
			return listStudent;
		} catch(DataAccessException e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
		
	}

}
